package AIRProject.filemanager
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author FireKnight
	 */
	public class OpenFile extends EventDispatcher
	{
		
		public function OpenFile()
		{
		
		}
		
		/**
		 * 打开文件夹
		 * @param browseHandler
		 * @param title
		 *
		 */
		public static function browseForDirectory(browseHandler:Function, title:String = ""):void
		{
			new BrowseForDirectory(title, browseHandler);
		}
		
		/**
		 * 打开文件
		 * @param browseHandler
		 * @param title
		 * @param fileTypes
		 *
		 */
		public static function browseForOpen(browseHandler:Function, title:String = "", fileTypes:Array = null):void
		{
			new BrowseForOpen(fileTypes, title, browseHandler);
		}
		
		/**
		 *
		 * @param browseHandler
		 * @param title
		 * @param fileTypes
		 *
		 */
		public static function browseForOpenMultiple(browseHandler:Function, title:String = "", fileTypes:Array = null):void
		{
			new BrowseForOpenMultiple(fileTypes, title, browseHandler);
		}
		
		/**
		 * 保存文件
		 * @param browseHandler
		 * @param title
		 *
		 */
		public static function browseForSave(browseHandler:Function, title:String = ""):void
		{
			new BrowseForSave(title, browseHandler);
		}
		
		/**
		 * 获得文件夹所有文件
		 * @param path
		 * @param func   func为空 那就直接返回结果  func不为空 就异步返回结果
		 *                         func -> function(path:String,value:Array)
		 * @return
		 *
		 */
		public static function openDirectory(path:String, func:Function = null):Array
		{
			var file:File = new File(path);
			if (!file.exists || !file.isDirectory)
			{
				return null;
			}
			if (func == null)
			{
				return file.getDirectoryListing();
			}
			
			new GetDirectory(file, path, func);
			return [];
		}
		
		/**
		 * 打开文件
		 * @param file
		 * @return
		 *
		 */
		public static function open(file:File):ByteArray
		{
			if (!file || !file.exists)
			{
				return null;
			}
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var fileDate:ByteArray = new ByteArray();
			stream.readBytes(fileDate, 0, stream.bytesAvailable);
			stream.close();
			return fileDate;
		}
		
		/**
		 * 文本方式打开文件
		 * @param file
		 * @param type
		 * @return
		 *
		 */
		public static function openAsTxt(file:File, type:String = null):String
		{
			var b:ByteArray = open(file);
			if (!b)
			{
				return null;
			}
			b.position = 0;
			type = type ? type : getFileType(b);
			//trace("type="+type);
			b.position = 0;
			return b.readMultiByte(b.bytesAvailable, type);
		}
		
		public static function openAsUtfTxt(file:File, type:String = null):String
		{
			var b:ByteArray = open(file);
			if (!b)
			{
				return null;
			}
			b.position = 0;
			return b.readMultiByte(b.bytesAvailable, "utf-8");
		}
		
		/**
		 * object方式打开文件 数据可以保存为amf格式
		 * @param file
		 * @param inflate
		 * @return
		 *
		 */
		public static function openAsObj(file:File, inflate:Boolean = false):*
		{
			var b:ByteArray = open(file);
			if (!b)
			{
				return null;
			}
			b.position = 0;
			if (inflate)
			{
				b.inflate();
				b.position = 0;
			}
			return b.readObject();
		}
		
		public static function getFileType(bytes:ByteArray):String
		{
			var b0:int = bytes.readUnsignedByte();
			var b1:int = bytes.readUnsignedByte();
			bytes.position = 0;
			if (b0 == 0xFF && b1 == 0xFE)
			{
				return "unicode";
			}
			else if (b0 == 0xFE && b1 == 0xFF)
			{
				return "unicodeFFFE";
			}
			else if (b0 == 0xEF && b1 == 0xBB)
			{
				return "utf-8";
			}
			else
			{
				return "gb2312"
			}
		
		}
		
		/**
		 * 写文件
		 * @param data
		 * @param path
		 * @return
		 *
		 */
		public static function write(data:*, path:String,_file:File=null):File
		{
			
			var stream:FileStream;
			var byte:ByteArray
			if (data is ByteArray)
			{
				byte = data as ByteArray;
			}
			else if (data is String)
			{
				byte = new ByteArray();
				byte.writeUTFBytes(data);
			}
			else
			{
				byte = new ByteArray();
				byte.writeObject(data);
			}
			
			byte.position = 0;
			
			try
			{
				if(_file!=null)
				{
					
				}
				else
				{
					_file = new File(path);
				}
				
				stream = new FileStream();
				stream.open(_file, FileMode.WRITE);
				stream.writeBytes(byte);
				stream.close();
				return _file;
			}
			catch (e:Error)
			{
				trace("write file failed");
				
			}
			return null;
		}
	
	}

}